<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI Chat Assistant</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <style>
        :root {
            --primary-color: #2563eb;
            --ai-bubble: #f1f5f9;
            --user-bubble: #3b82f6;
        }

        body {
            background: #f8fafc;
            font-family: 'Segoe UI', system-ui, sans-serif;
        }

        .container {
            max-width: 800px;
            margin: 2rem auto;
            padding: 1rem;
            background: white;
            border-radius: 1rem;
            box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
        }

        .chat-header {
            text-align: center;
            padding: 1.5rem;
            border-bottom: 2px solid #e2e8f0;
        }

        .chat-header h1 {
            color: #1e293b;
            margin: 0;
            font-size: 1.8rem;
        }

        .chat-box {
            height: 60vh;
            padding: 1rem;
            overflow-y: auto;
            scroll-behavior: smooth;
        }

        .message {
            display: flex;
            gap: 1rem;
            margin-bottom: 1.5rem;
            animation: fadeIn 0.3s ease-in;
        }

        .message.ai {
            flex-direction: row;
        }

        .message.user {
            flex-direction: row-reverse;
        }

        .avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            flex-shrink: 0;
        }

        .ai .avatar {
            background: var(--primary-color);
            color: white;
        }

        .user .avatar {
            background: #94a3b8;
            color: white;
        }

        .content {
            max-width: 70%;
            padding: 1rem;
            border-radius: 1rem;
            line-height: 1.5;
        }

        .ai .content {
            background: var(--ai-bubble);
            border: 1px solid #e2e8f0;
        }

        .user .content {
            background: var(--user-bubble);
            color: white;
        }

        .input-group {
            display: flex;
            gap: 0.5rem;
            padding: 1rem;
            background: #f8fafc;
            border-radius: 0.5rem;
        }

        input[type="text"] {
            flex: 1;
            padding: 0.8rem 1rem;
            border: 2px solid #cbd5e1;
            border-radius: 0.5rem;
            outline: none;
            transition: border-color 0.2s;
        }

        input[type="text"]:focus {
            border-color: var(--primary-color);
        }

        button {
            padding: 0.8rem 1.5rem;
            background: var(--primary-color);
            color: white;
            border: none;
            border-radius: 0.5rem;
            cursor: pointer;
            transition: opacity 0.2s;
            display: flex;
            align-items: center;
            gap: 0.5rem;
        }

        button:hover {
            opacity: 0.9;
        }

        .loading {
            display: none;
            padding: 1rem;
            text-align: center;
            color: #64748b;
        }

        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(10px); }
            to { opacity: 1; transform: translateY(0); }
        }

        /* 美化滚动条 */
        ::-webkit-scrollbar {
            width: 8px;
        }
        ::-webkit-scrollbar-track {
            background: #f1f5f9;
        }
        ::-webkit-scrollbar-thumb {
            background: #cbd5e1;
            border-radius: 4px;
        }
        .meta {
            margin-top: 0.5rem;
            font-size: 0.8rem;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .time {
            color: #64748b;
        }

        .copy-btn {
            background: none;
            border: none;
            color: var(--primary-color);
            cursor: pointer;
            padding: 0.2rem 0.5rem;
            border-radius: 0.25rem;
        }

        .copy-btn:hover {
            background: #e2e8f0;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="chat-header">
        <h1>AI Chat Assistant</h1>
        <div class="loading" id="loading">AI正在思考中...</div>
    </div>

    <div class="chat-box" id="chatBox"></div>

    <div class="input-group">
        <input type="text"
               id="messageInput"
               placeholder="输入您的问题..."
               onkeypress="if(event.keyCode == 13) sendMessage()">
        <button onclick="sendMessage()">
            <i class="fas fa-paper-plane"></i>
            发送
        </button>
    </div>
</div>

<script>
    function createMessageElement(text, isUser) {
        const template = `
            <div class="message ${isUser ? 'user' : 'ai'}">
                <div class="avatar">${isUser ? '👤' : '🤖'}</div>
                <div class="content">
                    <div class="text">${text}</div>
                    <div class="meta">
                        <span class="time">${new Date().toLocaleTimeString()}</span>
                        ${!isUser ? `<button class="copy-btn" onclick="copyAnswer(this)">复制</button>` : ''}
                    </div>
                </div>
            </div>
        `;
        return new DOMParser().parseFromString(template, 'text/html').body.firstChild;
    }

    function sendMessage() {
        const input = document.getElementById('messageInput');
        const chatBox = document.getElementById('chatBox');
        const loading = document.getElementById('loading');

        if (!input.value.trim()) return;

        // 添加用户消息
        const userMsg = createMessageElement(input.value, true);
        chatBox.appendChild(userMsg);

        // 显示加载状态
        loading.style.display = 'block';
        const button = document.querySelector('button');
        button.disabled = true;

        fetch('/api/ai/chat', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ message: input.value })
        })
            .then(response => response.json())
            .then(data => {
                const aiMsg = createMessageElement(data.response, false);
                chatBox.appendChild(aiMsg);
                chatBox.scrollTo(0, chatBox.scrollHeight);
            })
            .catch(error => {
                const errorMsg = createMessageElement(`请求失败：${error.message}`, false);
                chatBox.appendChild(errorMsg);
            })
            .finally(() => {
                input.value = '';
                loading.style.display = 'none';
                button.disabled = false;
                input.focus();
            });
    }

    // 复制功能
    function copyAnswer(button) {
        const text = button.closest('.message').querySelector('.text').textContent;
        navigator.clipboard.writeText(text).then(() => {
            button.textContent = '已复制!';
            setTimeout(() => button.textContent = '复制', 2000);
        });
    }
</script>
</body>
</html>